package com.servlet.teacher;

import com.bean.BookBean;
import com.dao.BooksDao;
import com.dao.impl.BooksDaoImpl;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.util.JsonUtil;
import com.util.PageUtil;
import com.util.ProxyUtil;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;



@WebServlet("/DDBookServlet.do")
public class DDBookServlet extends BaseServlet {





    public void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        //  HttpSession session =  req.getSession();
      //  Object loginUserName =     session.getAttribute("loginUserInfo");

      //  System.out.println("从session中取出的值为:"+loginUserName);


        JsonUtil jsonUtil = new JsonUtil();
        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson =  gsonBuilder.create();



        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();




        BooksDao booksDao = (BooksDao)ProxyUtil.getDaoProxy(new BooksDaoImpl());



        //queryDataBypage方法的参数currentPage肯定通过前端传递过来
        //前端如何传递参数给Servlet呢  原来我们用的是提交表单实现的表单 数据是会附加在请求的url后面  ?key=value&key=value
        //   我们只要要url后面添加?key=value就可以传值。。
        String curentPage =   req.getParameter("currentPage");
        String bookName =req.getParameter("bookName");
        String bookType =   req.getParameter("bookType");

        BookBean bookBean  = new BookBean();
        bookBean.setBook_name(bookName);
        bookBean.setType_name(bookType);//把拿到的请求参数放到JavaBean中



        try {
            PageUtil<BookBean> bookBeanPageUtil =  booksDao.queryDataBypage(Integer.parseInt(curentPage),15,bookBean);

            jsonUtil.setMsg("成功");
            jsonUtil.setData(bookBeanPageUtil);


        } catch (Exception e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误:"+e.getMessage());

        }

        String jsonStr =   gson.toJson(jsonUtil);


        out.print(jsonStr);//把json字符串输出给客户端
    }

    public void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //执行保存操作

        System.out.println("调用了save方法");
    }


    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {





        String bookid =   req.getParameter("bookid");
        //调用Dao的删除方法
        BooksDao booksDao = (BooksDao)ProxyUtil.getDaoProxy(new BooksDaoImpl());

        JsonUtil jsonUtil = new JsonUtil();
        try {
          /*  String book_id =  req.getParameter("book_id");//分别取出前端传递过来的数据
            String book_name =  req.getParameter("book_name");
            String isbn =  req.getParameter("isbn");
            String author =  req.getParameter("author");
            String publish =  req.getParameter("publish");
            String publish_date =  req.getParameter("publish_date");
            String pages =  req.getParameter("pages");
            bookBean.setBook_id(Integer.parseInt(book_id));*/
            BookBean bookBean = new BookBean();


            //将客户端传递过来的请求参数封装到bookBean对象中
            //要求：参数名必需与bookBean中的属性名有对应关系
            System.out.println("反射前的BookBean对象中的属性值为:"+bookBean);
            BeanUtils.populate(bookBean,req.getParameterMap());
            System.out.println("反射后的BookBean对象中的属性值为:"+bookBean);

            int res =  booksDao.update(bookBean);

            jsonUtil.setCode(0);
            jsonUtil.setMsg("成功");






        } catch (SQLException e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误:"+e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误:"+e.getMessage());
        }

        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson =  gsonBuilder.create();

        String jsonStr =  gson.toJson(jsonUtil);


        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.print(jsonStr);
    }




    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String bookid =   req.getParameter("bookid");
        //调用Dao的删除方法
        BooksDao booksDao = (BooksDao)ProxyUtil.getDaoProxy(new BooksDaoImpl());

        JsonUtil jsonUtil = new JsonUtil();
        try {
            int res =  booksDao.deleteById(bookid);

            jsonUtil.setCode(0);
            jsonUtil.setMsg("成功");
        } catch (SQLException e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误:"+e.getMessage());
        }

        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson =  gsonBuilder.create();

        String jsonStr =  gson.toJson(jsonUtil);


        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.print(jsonStr);

    }
}
